﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Infrastructure;

namespace FarmSmartSleep.Organ.API.Application.Commands.EnterpriseCommands
{
    public class EnterTree
    {
        public IBaseRepository<AccountingUnits> accountingUnitsRepository;

        public EnterTree(IBaseRepository<AccountingUnits> accountingUnitsRepository)
        {
            this.accountingUnitsRepository = accountingUnitsRepository;
        }

        public List<object> GetTree(int Pid = 0)
        {
            var list = accountingUnitsRepository.GetAll().Where(x => x.Pid == Pid).ToList();
            var result = list.Select(x => new
            {
                Value = x.AccountingUnitId,
                Label = x.AccountingUnitName,
                Children = GetTree(x.AccountingUnitId).Count() > 0 ? GetTree(x.AccountingUnitId) : null
            }).Distinct().ToList<object>();
            return result;
        }

    }
}
